package com.eascs.ticket.ticket.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.ticket.common.entity.StatusConstant;
import com.eascs.ticket.common.service.BaseSevice;
import com.eascs.ticket.ticket.entity.TicketWatcher;
import com.eascs.ticket.ticket.entity.TicketWatcherDelete;

@Service
public class TicketWatcherService extends BaseSevice {
	@Inject
	private TicketChangeLogService ticketChangeLogService;

	public List<Object[]> getTicketWatcherByTicketId(String ticketId) {
		return this.proxyDao.findObjectsList("select id, watcher_user_code,updator,update_date from ticket_watcher where ticket_id=?  and status<> ? order by creator", ticketId, StatusConstant.STATUS_DEL);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void insertTicketWatcher(TicketWatcher ticketWatcher) {
		if (this.proxyDao.findInteger("select count(1) from ticket_watcher where status = ? and ticket_id=? and watcher_user_code=?", StatusConstant.STATUS_OK, ticketWatcher.getTicketId(), ticketWatcher.getWatcherUserCode()) > 0) {
			return;
		}
		this.proxyDao.insert(ticketWatcher);
		ticketChangeLogService.insertTicketChangeLogWithObject(ticketWatcher.getTicketId(), "增加关注", ticketWatcher);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void deleteTicketWatcher(TicketWatcherDelete ticketWatcherDelete) {
		TicketWatcher ticketWatcher = this.getTicketWatcherById(ticketWatcherDelete.getId());
		ticketChangeLogService.insertTicketChangeLogWithObject(ticketWatcher.getTicketId(), "删除关注", ticketWatcherDelete);
		this.proxyDao.deleteById(TicketWatcherDelete.class, ticketWatcherDelete.getId());
	}

	public TicketWatcher getTicketWatcherById(String id) {
		return this.proxyDao.getById(TicketWatcher.class, id);
	}
}
